package com.nxy.cpfsfe.busi.bbatproc;


import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adtec.starring.datapool.CompSDO;
import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.nxy.cpfsfe.PubBeanSerial;
import com.nxy.cpfsfe.cache.DbToParmToCache;
import com.nxy.cpfsfe.enumtype.Constant;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.enumtype.RpcOutputInfoEnum;
import com.nxy.cpfsfe.enumtype.SysEcodeEnum;
import com.nxy.cpfsfe.mybatis.mapper.ToOrgMapper;
import com.nxy.cpfsfe.mybatis.pojo.ToOrg;
import com.nxy.cpfsfe.mybatis.pojo.ToParm;
import com.nxy.cpfsfe.util.DateUtil;
import com.nxy.cpfsfe.util.RpcCommUtil;
import com.nxy.cpfsfe.util.XMLNodeUtil;
/**
 * 
 * Description: 处理系统登录请求NPS.805
 * @author wp
 * @date 2019年9月12日
 */
public class BBatLgnReqSrvBean {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(BBatLgnReqSrvBean.class);
	SqlSession sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");	
	
	
	public void bBatLgnReqSrv() {
		String bizMsg=null;
		Map<String, Object> map = null;
		String tpId = DtaInfo.getInstance().getTpId();	
		// 获取输入
		CompSDO inputSdo = (CompSDO) EPOper.getCompSDO(tpId, "ReqBody"); // 获取输入接口		
		bizMsg = (String) inputSdo.getValue("BizMsg")!=null?(String) inputSdo.getValue("BizMsg"):"";
		//String sndAppCd = (String) EPOper.get(tpId, "ReqBean[0].head[0].sndAppCd");
		LOGGER.debug("接收到输入BizMsg报文数据:[{}]" , bizMsg);
		JSONObject jsonObject = JSONObject.parseObject(bizMsg);
		String sndMbrCd = jsonObject.getString("orgBNo");
		String loginOprTp = jsonObject.getString("ncsSta");
		LOGGER.debug("发送成员行行号:[{}]" , sndMbrCd);
		LOGGER.debug("系统请求操作类型:[{}]" , loginOprTp);
		try{			
			returnGWNPS805(sndMbrCd,loginOprTp);
		}catch(Exception e){
			LOGGER.error("调用网关转发NPS.805报文服务失败",e);
			throw new BaseException(RespCd.NCPO6047,e,"调用网关服务异常");
		}
		
		//初始化处理
		ToOrg toOrg = new ToOrg();
		toOrg.setOrgBNo(sndMbrCd);
		ToOrgMapper toOrgMap = sqlSession.getMapper(ToOrgMapper.class);
		ToOrg toOrgOb = toOrgMap.queryByBNo(toOrg);
		String ncsSta = toOrgOb.getNcsSta();
		if(ncsSta == null || "".equals(ncsSta)){
			toOrg.setNcsSta("OT09");     //处理中
			toOrg.setUpdTm(new Date());
			toOrgMap.update(toOrg);
		}
		//向内管返回信息
		CompSDO outputSdo = EPOper.getCompSDO(tpId, "RspBody"); // 获取输出接口
		EPOper.put(tpId, "RspBean[0].head[0].rspsCd", "0000000");
		EPOper.put(tpId, "RspBean[0].head[0].respsInfoDesc", "处理中");
		List<Object> list = new ArrayList<Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        String msg = "正在处理中，请稍后";
        map1.put("BizMsg",msg);
        JSONObject json = new JSONObject();
        json = new JSONObject(map1);
        list.add(json);
		outputSdo.setValue("BizMsg", JSONObject.toJSONString(list));
	}
	
	public void returnGWNPS805(String sndMbrCd,String loginOprTp){
		String tpId = DtaInfo.getInstance().getTpId();		
		String bizMsg805 = buildLgnXml(sndMbrCd,loginOprTp);
		//转发给网关
	    LOGGER.debug("rpc调用网关发送NPS805开始......");
	    EPOper.put(tpId, "ReqBean[0].head[0].sndAppCd", SysEcodeEnum.CPFS.getCode());
	    EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd", SysEcodeEnum.NCS.getCode());
    	EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", MsgCdOrTpEnum.MSGCD_805.getCode());
    	String seqNo = PubBeanSerial.getInstance().getSeqNoAndIncrea(SysEcodeEnum.CPFS.getCode());
    	EPOper.put(tpId, "ReqBean[0].head[0].initrSeqno",seqNo);
	    EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg", bizMsg805);   
		RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());	
	}
	
		

	// 组装NPS.805报文
	public static String buildLgnXml(String sndMbrCd,String loginOprTp) {
		LOGGER.info("---------系统登录/退出报文--------");
		try {

			XMLNodeUtil xmlReqDoc;
			xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,  SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP+"NPS_805.xml");
			Date date = new Date();
			String seqNb = PubBeanSerial.getInstance().getSeqNoAndIncrea("seqNb");
			String msgId = PubBeanSerial.getInstance().getSeqNoAndIncrea("MSGID");
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndAppCd","MPS");
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndMbrCd",sndMbrCd);
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndDt",DateUtil.getDate());
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndTm",DateUtil.getTime());
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/rcvAppCd","NPS");
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/rcvMbrCd","100000000000");
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/seqNb",seqNb);
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/structType","XML");
			xmlReqDoc.setNodeContentTrim("/transaction/header/msg/callTyp","ASYN");
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/MsgId",msgId);
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/CreDtTm",DateUtil.getISODateTime(date));
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/InstgPty/InstgDrctPty",sndMbrCd);
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/InstgPty/InstgPty",sndMbrCd);
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/InstdPty/InstdDrctPty","100000000000");
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/InstdPty/InstdPty","100000000000");
			/*xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/BizTyp","");
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/BizKind","");*/
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/TranChnlTyp","06");
			//xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/GrpHdr/Rmk","");
			xmlReqDoc.setNodeContentTrim("/transaction/body/LoginReq/LoginInf/LoginOprTp",loginOprTp);
			xmlReqDoc.setNodeContentTrim("/transaction/signature","");
			String msg = xmlReqDoc.toString();
			msg = msg.replace("<transaction>", "<transaction xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:ncs2:std:nps:2012:tech:xsd:NPS.805.001.01\">");
			return msg;

		} catch (Throwable e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			throw new BaseException("返回登录/退出响应应答失败",e);
		}
	}

}
